package test;

import java.util.ArrayList;

import graphSample.SerialMachine.SerialMachineTC;

import core.Graph;
import core.GraphBalancer;
import core.Node;
import core.NodeScanner;

public class RunGraph extends Thread{
	
	static ArrayList<Node> availableNodeList = new ArrayList<Node>();
	
	public static void main(String[] argv){
		NodeScanner ns = new NodeScanner();
		ns.start();
		
		try {
			sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		ns.setDone();
		
		Node.printNodeList();
		Node.printNodeDist();
		
		// 记录当前所有可用节点
		
		availableNodeList.addAll(Node.NodeList);
		
		//run(availableNodeList.size());
		
		
		for(int i=1; i<=1; i++){
			run(availableNodeList.size(),100000,10000);
		}
	
	}
	
	static void run(int nodeNum, int maxTime, int scanInterval){
		
		Graph g = new SerialMachineTC(0,new int[]{2,2,2,2},new double[]{18,18,18,18,18},2).g;//TwoMachineTR.getGraph(2);
		
		Node.NodeList = new ArrayList<Node>();
		
		for(int i=0; i<nodeNum; i++){
			Node.NodeList.add(availableNodeList.get(i));
		}
		
		
		System.out.println("...Print graph brief info");
		g.printNameAndId();
		
		System.out.println("...Analysis this graph");
		// Analysis this graph
		g.anls();
		g.printCluster();
		g.printWorkload();
		g.printRela();
		
		System.out.println("...Produce assignment plan");
		// Produce assignment plan
		g.preAssign();
		g.printPreAssignment();
		
		System.out.println("...Reset all the Nodes");
		// Reset all the Nodes
		Node.resetAll();
		
		System.out.println("...Assign the Entities and Put the initial Tokens");
		// Assign the Entities and Put the initial Tokens
		g.assignAndImplAllEntities();
		
		System.out.println("...Implement all Process");
		// Implement all Process
		g.implAllProc();
		
		// TODO: Test the Graph Balancer
		GraphBalancer gb = new GraphBalancer(g,scanInterval);
		gb.start();
			
		System.out.println("...Waiting sometime");
		try {
			sleep(maxTime);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		gb.setDone();
		try {
			sleep(scanInterval*3);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.println("...Unload All Process");
		// Unload All Process
		g.unloadAllProc();
		
		System.out.println("...Get Evaluation Results");
		// Get Evaluation Results
		g.getResult();
		
		System.out.println("...Unload all Entities and Generate results");
		// Unload all Entities and generate results
		g.unloadAllEntities();
		g.printResultToFile();
		
		System.out.println("...Done");
	}
}
